<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');
?>


<?php
$building_id = mysql_real_escape_string(strip_tags($_GET['bid']));
$instructor_id = mysql_real_escape_string(strip_tags($_GET['iid']));

$get_building_data = mysql_query("SELECT * FROM buildings WHERE id = $building_id");
$building_name = mysql_result($get_building_data,0,'building_name');
$abbreviation = mysql_result($get_building_data,0,'abbreviation');

$query = "SELECT * FROM instructors WHERE id = '$instructor_id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$fname = $row['first_name'];
$lname = $row['last_name'];

$query="SELECT *, tsp.id AS pref_id
		FROM time_slot_preferences tsp
		JOIN time_slots ts ON ts.id = tsp.time_slot_id
		WHERE building_id = '$building_id'
		AND instructor_id = '$instructor_id'
		ORDER BY rank_of_preference ASC";
$result=mysql_query($query);
$num_rows=mysql_num_rows($result);

if (isset($_GET['up']))
{
	$pref_id = mysql_real_escape_string(strip_tags($_GET['pref_id']));
	$query = "SELECT rank_of_preference FROM time_slot_preferences WHERE id = '$pref_id'";
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	$rank = $row['rank_of_preference'];
	
	if ($rank != 1)
	{
		$query = "SELECT id FROM time_slot_preferences WHERE rank_of_preference = ($rank - 1)";
		$result = mysql_query($query);
		$row = mysql_fetch_assoc($result);
		$moving_pref_id = $row['id'];
		
		$query = "UPDATE time_slot_preferences
				  SET rank_of_preference = '$rank'
				  WHERE id = '$moving_pref_id'";
		$result = mysql_query($query);
		
		$query = "UPDATE time_slot_preferences
				  SET rank_of_preference = ($rank - 1)
				  WHERE id = '$pref_id'";
		$result = mysql_query($query);
	}
}
if (isset($_GET['down']))
{
	$pref_id = mysql_real_escape_string(strip_tags($_GET['pref_id']));
	$query = "SELECT rank_of_preference FROM time_slot_preferences WHERE id = '$pref_id'";
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	$rank = $row['rank_of_preference'];
	
	if ($rank != $num_rows)
	{
		$query = "SELECT id FROM time_slot_preferences WHERE rank_of_preference = ($rank + 1)";
		$result = mysql_query($query);
		$row = mysql_fetch_assoc($result);
		$moving_pref_id = $row['id'];
		
		$query = "UPDATE time_slot_preferences
				  SET rank_of_preference = '$rank'
				  WHERE id = '$moving_pref_id'";
		$result = mysql_query($query);
		
		$query = "UPDATE time_slot_preferences
				  SET rank_of_preference = ($rank + 1)
				  WHERE id = '$pref_id'";
		$result = mysql_query($query);
	}
}

echo "<p><a href=\"update_preferences.php?id=$instructor_id\">&lt;&lt; Back to Buildings</a></p>";
echo "<h1>$fname $lname Timeslot Preferences for $abbreviation</h1>";
echo '<p>';
//creates link/button for add timeslot
echo "<a href='add_pref.php?iid=$instructor_id&amp;bid=$building_id'>Add timeslot</a>";
echo '</p>';


//select statement
$query="SELECT *, tsp.id AS pref_id
		FROM time_slot_preferences tsp
		JOIN time_slots ts ON ts.id = tsp.time_slot_id
		WHERE building_id = '$building_id'
		AND instructor_id = '$instructor_id'
		ORDER BY rank_of_preference ASC";
$result=mysql_query($query);

//determine dynamically number of rows read from table
$num_rows=mysql_num_rows($result);

if ($num_rows > 0)
{?>


	
	<table class="postSchedule">
		<tr>
			<th>Rank</th>
            <th>Days</th>
            <th>Start Time</th>
            <th colspan="3"></th>
		</tr>
<?php
	//loop to walk through and print to screen all entries in timeslots table
	$row_count=0;
	while ($row = mysql_fetch_assoc($result)) 
	{
		$row_count++;
		//store each timeslotname and abbreviation in a scalar
		$pref_id = $row['pref_id'];
		$rank = $row['rank_of_preference'];
		$starttime=$row["start_time"];
		$endtime=$row["end_time"];
		$mon=$row["Monday"];
		$tues=$row["Tuesday"];
		$weds=$row["Wednesday"];
		$thurs=$row["Thursday"];
		$fri=$row["Friday"];
		$sat=$row["Saturday"];
		$sun=$row["Sunday"];
	
		//generate table row for each timeslot on each iteration of the loop
		echo "<tr";
		if (($row_count%2) != 0) echo " class=\"zebra_stripe\"";
		echo ">";
			echo "<td>$rank</td>";	
			
			$days_of_week = '';
			if ($row['Monday'])
				$days_of_week .= 'M,';
			if ($row['Tuesday'])
				$days_of_week .= 'Tu,';
			if ($row['Wednesday'])
				$days_of_week .= 'W,';
			if ($row['Thursday'])
				$days_of_week .= 'Th,';
			if ($row['Friday'])
				$days_of_week .= 'F,';
			if ($row['Saturday'])
				$days_of_week .= 'Sa,';
			if ($row['Sunday'])
				$days_of_week .= 'Su,';
			$days_of_week = substr_replace($days_of_week, '', -1, 1);
			echo "<td>$days_of_week</td>";
			
			echo "<td>".date('g:ia', strtotime($starttime))." - ".date('g:ia', strtotime($endtime))."</td>";
			
			echo "<td>";
			if ($num_rows > 1 && $row_count != 1)
			{
				echo "<a href=\"update_preferences_second.php?bid=$building_id&amp;iid=$instructor_id&amp;pref_id=$pref_id&amp;up=true\">Move Up</a>";
			}
			echo "</td>";
			
			echo "<td>";
			if ($num_rows > 1 && $row_count != $num_rows)
			{
				echo "<a href=\"update_preferences_second.php?bid=$building_id&amp;iid=$instructor_id&amp;pref_id=$pref_id&amp;down=true\">Move Down</a>";
			}
			echo "</td>";
			
			echo "<td><a href=\"delete_pref.php?bid=$building_id&amp;iid=$instructor_id&amp;pref_id=$pref_id\">Delete</a></td>";
	
		echo "</tr>";
		
	}

	?>
	</table>
    
<?php
}
else
{
	echo "<p>There are no timeslot preferences for this instructor in this building.</p>";
}



?>


<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>